001    /* $RCSfile: RSAKeyFactoryEngine.java,v $
002     * $Revision: 1.8 $
003     * $Date: 2003/10/04 19:18:38 $
004     * $Author: uwe_guenther $
005     * $State: Exp $
006     *
007     * Created on November 9, 2001 1:49 PM
008     *
009     * Copyright (C) 2001  Uwe Guenther  <uwe@cscc.de >
010     *
011     * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE-
012     * ServiceProvider is a library, written in JavaTM, that should be 
013     * used in HBCI banking applications (clients and may be servers),
014     * to do cryptographic operations.
015     *
016     * The jhbci library is free software; you can redistribute it and/or
017     * modify it under the terms of the GNU Lesser General Public
018     * License as published by the Free Software Foundation; either
019     * version 2.1 of the License, or (at your option) any later version.
020     *
021     * The jhbci library is distributed in the hope that it will be useful,
022     * but WITHOUT ANY WARRANTY; without even the implied warranty of
023     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
024     * Lesser General Public License for more details.
025     *
026     * You should have received a copy of the GNU Lesser General Public
027     * License along with this library; if not, write to the Free Software
028     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
029     *
030     */
031    
032    package de.cscc.crypto.provider;
033    
034    import java.security.InvalidKeyException;
035    import java.security.Key;
036    import java.security.KeyFactorySpi;
037    import java.security.PrivateKey;
038    import java.security.PublicKey;
039    import java.security.spec.InvalidKeySpecException;
040    import java.security.spec.KeySpec;
041    
042    /** 
043     * RSAKeyFactoryEngine Class.
044     *
045     * @author  <a href=mailto:uwe@cscc.de >Uwe Günther </a>
046     *
047     * @version $Revision: 1.8 $
048     */
049    public final class RSAKeyFactoryEngine extends KeyFactorySpi {
050    
051        /** The delegate for this wrapper object */
052        private RSAKeyFactoryImpl factory = new RSAKeyFactoryImpl();    
053        
054        /** Creates new RSAKeyFactoryEngine */
055        public RSAKeyFactoryEngine() {
056            if (JHBCI.selfIntegrityChecking() == false) {
057                throw new SecurityException("JHBCI-Provider is tampered.");
058            }        
059        }
060    
061        /**
062         * Returns a string representation of the object. 
063         *
064         * @return  a string representation of the object.
065         */
066        public String toString() {
067            return this.factory.toString();
068        }    
069        
070        /**
071         * Generates a private key object from the provided key
072         * specification (key material).
073         *
074         * <p>The following KeySpecs will produce the following instances of its 
075         * matching keys:
076         * <ul>
077         * <li> RSAPrivateCrtKeySpec produces:
078         * <ul>
079         * <li>a instance of RSAPrivateCrtKey</li>
080         * </ul>
081         * </li>
082         * <li> RSAPrivateKeySpec produces:
083         * <ul>
084         * <li>a instance of RSAPrivateKey</li>
085         * </ul>
086         * </li>
087         * </ul>
088         *
089         * @param keySpec the specification (key material) of the private key.
090         *
091         * @return the private key.
092         *
093         * @throws InvalidKeySpecException if the given key specification
094         * is inappropriate for this key factory to produce a private key.
095         * @throws NullPointerException If <code>keySpec</code> or one of the key 
096         * data in <code>keySpec</code> is <code>null</code>. So you have to 
097         * construct a valid keySpec without <code>null</code> fields.
098         * @see java.math.BigInteger
099         */
100        public PrivateKey engineGeneratePrivate(KeySpec keySpec) 
101                throws InvalidKeySpecException {
102            return this.factory.generatePrivate(keySpec);
103        }
104        
105        /**
106         * Generates a public key object from the provided key
107         * specification (key material).
108         *
109         * <p>The following KeySpecs will produce the following instances of its 
110         * matching keys:
111         * <ul>
112         * <li> RSAPublicKeySpec produces:
113         * <ul>
114         * <li>a instance of RSAPublicKey</li>
115         * </ul>
116         * </li>
117         * </ul>
118         *
119         * @param keySpec the specification (key material) of the public key.
120         *
121         * @return the public key.
122         *
123         * @throws InvalidKeySpecException if the given key specification
124         * is inappropriate for this key factory to produce a public key.
125         * @throws NullPointerException If <code>keySpec</code> or one of the key
126         * data in <code>keySpec</code> is <code>null</code>. So you have to 
127         * construct a valid keySpec without <code>null</code> fields.
128         * @throws IllegalArgumentException if any key material from keySpec is 
129         * negative.
130         */
131        public PublicKey engineGeneratePublic(KeySpec keySpec) 
132                throws InvalidKeySpecException {
133            return this.factory.generatePublic(keySpec);
134        }
135        
136        /**
137         * Returns a specification (key material) of the given key
138         * object.
139         * <code>keySpec</code> identifies the specification class in which
140         * the key material should be returned. It could, for example, be
141         * <code>RSAPublicKeySpec.class</code>, to indicate that the
142         * key material should be returned in an instance of the
143         * <code>RSAPublicKeySpec</code> class.
144         *
145         * <p>The following conversions are valid for this provider:
146         * <ul>
147         * <li>RSAPrivateCrtKey:
148         * <ul>
149         * <li>to RSAPrivateCrtKeySpec</li>
150         * <li>to RSAPrivateKeySpec</li>
151         * <li>to RSAPublicKeySpec</li>
152         * </ul>
153         * </li>
154         * <li>RSAPrivateKey:
155         * <ul>
156         * <li>to RSAPrivateKeySpec</li>
157         * </ul>
158         * </li>
159         * <li>RSAPublicKey:
160         * <ul>
161         * <li>to RSAPublicKeySpec</li>
162         * </ul>
163         * </li>
164         * </ul>
165         *
166         * @param key the key.
167         *
168         * @param keySpec the specification class in which
169         * the key material should be returned.
170         *
171         * @return the underlying key specification (key material) in an instance
172         * of the requested specification class.
173         *
174         * @throws InvalidKeySpecException if the requested key specification is
175         * inappropriate for the given key, or the given key cannot be dealt with
176         * (e.g., the given key has an unrecognized format).
177         * @throws NullPointerException If one of the params or one of the needed 
178         * key data in the <code>key</code> is <code>null</code>. So you have to 
179         * produce a valid key without <code>null</code> fields with your 
180         * KeyPairGenerator. This should not be a problem with keys from this 
181         * provider, but maybe with keys from other providers.
182         * @throws IllegalArgumentException if any key material in the key
183         * <code>key</code> is negative. This should not be a problem with keys 
184         * from this provider, but maybe with keys from other providers.
185         */
186        public KeySpec engineGetKeySpec(Key key, Class keySpec) 
187                throws InvalidKeySpecException {
188            return this.factory.getKeySpec(key, keySpec);            
189        }
190        
191        /**
192         * Translates a key object, whose provider may be unknown or
193         * potentially untrusted, into a corresponding key object of this key
194         * factory.
195         *
196         * <p>The following translations are valid for this provider:
197         * <ul>
198         * <li>RSAPrivateCrtKey:
199         * <ul>
200         * <li>to RSAPrivateCrtKey</li>
201         * </ul>
202         * </li>
203         * <li>RSAPrivateKey:
204         * <ul>
205         * <li>to RSAPrivateKey</li>
206         * </ul>
207         * </li>
208         * <li>RSAPublicKey:
209         * <ul>
210         * <li>to RSAPublicKey</li>
211         * </ul>
212         * </li>
213         * </ul>
214         *
215         * @param key the key whose provider is unknown or untrusted.
216         *
217         * @return the translated key.
218         *
219         * @throws InvalidKeyException if the given key cannot be processed
220         * by this key factory.
221         * @throws NullPointerException If the <code>key</code> it self or one of 
222         * the key data in the <code>key</code> is <code>null</code>. So you have to
223         * produce a valid key without <code>null</code> fields with your 
224         * KeyPairGenerator. This should not be a problem with this provider, but 
225         * maybe with other providers.
226         * @throws IllegalArgumentException if any key material in the key
227         * <code>key</code> is negative. This should not be a problem with keys 
228         * from this provider, but maybe with keys from other providers.     
229         */
230        public Key engineTranslateKey(Key key) throws InvalidKeyException {
231            return this.factory.translateKey(key);
232        }
233    }